package com.game.controller;

import com.game.config.Coefficient;
import com.game.config.Numerical;
import com.game.data.BagData;
import com.game.data.RoleData;
import com.game.model.*;
import com.game.model.equip.Equip;
import com.game.model.api.EquipType;
import com.game.model.api.Move;
import com.game.model.role.PlayerRole;
import com.game.model.role.Role;
import com.game.model.equip.PlayerEquip;
import com.game.model.map.GameMap;
import com.game.view.Message;

import java.util.Iterator;
import java.util.Map;

/**
 * @Description:角色控制器
 **/
public class RoleController {
    //移动
    public static GameMap moveTo(GameMap gameMap, Move move, Role role) {
        int X = MapController.getRoleInMap(gameMap).getX();
        int Y = MapController.getRoleInMap(gameMap).getY();
        switch (move) {
            case UP:
                if (X - 1 < 0) {
                    //越界
                    Message.error("角色越界,无法行走");
                    return gameMap;
                } else {
                    //清除原来的角色位置
                    gameMap = MapController.removeObjectInMap(gameMap, X, Y, role);
                    //添加新的角色位置
                    gameMap = MapController.addObjectInMap(gameMap, X - 1, Y, role);
                }
                break;
            case DOWN:
                if (X + 1 > gameMap.getMapPoints().length - 1) {
                    //越界
                    Message.error("角色越界,无法行走");
                    return gameMap;
                } else {
                    //清除原来的角色位置
                    gameMap = MapController.removeObjectInMap(gameMap, X, Y, role);
                    //添加新的角色位置
                    gameMap = MapController.addObjectInMap(gameMap, X + 1, Y, role);
                }
                break;
            case LEFT:
                if (Y - 1 < 0) {
                    //越界
                    Message.error("角色越界,无法行走");
                    return gameMap;
                } else {
                    //清除原来的角色位置
                    gameMap = MapController.removeObjectInMap(gameMap, X, Y, role);
                    //添加新的角色位置
                    gameMap = MapController.addObjectInMap(gameMap, X, Y - 1, role);
                }
                break;
            case RIGHT:
                if (Y + 1 > gameMap.getMapPoints()[0].length - 1) {
                    //越界
                    Message.error("角色越界,无法行走");
                    return gameMap;
                } else {
                    //清除原来的角色位置
                    gameMap = MapController.removeObjectInMap(gameMap, X, Y, role);
                    //添加新的角色位置
                    gameMap = MapController.addObjectInMap(gameMap, X, Y + 1, role);
                }
                break;
        }
        return gameMap;
    }

    //获取计算后属性的角色信息
    public static Role getRole(Role role){
        role = role.deepClone();
        //计算角色与等级属性
        long HP = Numerical.calculateRoleBase(role.getLevel(),role.getBaseProperty().getHP());
        long MP = Numerical.calculateRoleBase(role.getLevel(),role.getBaseProperty().getMp());
        long ATK = Numerical.calculateRoleBase(role.getLevel(),role.getBaseProperty().getATK());
        long DEF = Numerical.calculateRoleBase(role.getLevel(),role.getBaseProperty().getDEF());
        int HIT = Numerical.calculateRoleSpecial(role.getLevel(),role.getSpecialProperty().getHIT());
        int CRI = Numerical.calculateRoleSpecial(role.getLevel(),role.getSpecialProperty().getCRI());
        int AVO = Numerical.calculateRoleSpecial(role.getLevel(),role.getSpecialProperty().getAVO());
        int CHR = Numerical.calculateEquipSpecial(role.getLevel(),role.getSpecialProperty().getCHR());
        role.setBaseProperty(new BaseProperty(HP,MP,ATK,DEF));
        role.setSpecialProperty(new SpecialProperty(HIT,CRI,AVO,CHR));
        //把装备的属性加到角色属性里来
        Iterator<Map.Entry<EquipType, Equip>> equipMap = RoleData.equipMap.entrySet().iterator();
        while (equipMap.hasNext()){
            Map.Entry<EquipType, Equip> next = equipMap.next();
            if (next.getValue() !=null){
                next.getValue().equipRole(role);
            }
        }
        return role;
    }
    //穿装备
    public static boolean putInEquip(Equip equip){
        try {
            Iterator<Map.Entry<EquipType, Equip>> iterator = RoleData.equipMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<EquipType, Equip> next = iterator.next();
                if (equip instanceof PlayerEquip){
                    //如果装备类型相同就替换原来身上的装备
                    if (next.getKey()==((PlayerEquip) equip).getEquipType()){
                        //把身上的装备脱下来 放到背包
                        if (next.getValue()!=null){
                            BagData.bag.addToBag(next.getValue());
                        }
                        //穿上新的装备
                        next.setValue(equip);
                    }
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //升级
    public static void upPlayerRole() {

        int currentLevel = RoleData.playerRole.getLevel();
        double cof = Math.pow(1 + Coefficient.EXPLEVEL, currentLevel);
        long neddExp = (long) (Coefficient.EXPINIT * cof);
        Role role = RoleController.getRole(RoleData.playerRole);
        long exp = ((PlayerRole) role).getExp();
        if (exp<neddExp){
            Message.error("经验不足");
        }else{
            Message.success("升级成功");
            RoleData.playerRole.setLevel(currentLevel+1);
        }
    }
}
